<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', 'copy', bomCost('purpat:save') && 'newy', 'sub', 'batchFill', 'remove', 'option', 'pass']"
      :disab-arr="['close', 'cancel', 'freeze']"
      url="tPurPat/queryTPurPatList"
      @copyDa="copyOrder()"
      @refresh="handleGetPurchaseList"
      @newly="addPurchase"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @del="delCustom"
      @startUsing="using(1)"
      @forbidden="using(2)"
      @handleCom1="queryReportForm1(1)"
      @handleCom2="queryReportForm1(2)"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQueryUntreated"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQueryUntreated"
        >
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQueryUntreated"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        card-tab
        table-selection
        get-row-keys="fentryId"
        @selectionChange="selectionDatas"
        @clickRow="detailPurchase"
        @search="handleQueryBomList1"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="detailPurchase(col.scope.row)">{{ col.scope.row.fbillNo }}</el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <!--  审核状态  -->
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentStatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">
            {{ col.scope.row.fdocumentStatus }}
          </el-tag>
        </template>
        <!--  禁用状态  -->
        <template v-slot:btnStates="col">
          <el-tag v-if="col.scope.row.fforbidStatus === '否'">{{ col.scope.row.fforbidStatus }}</el-tag>
          <el-tag v-else type="danger">{{ col.scope.row.fforbidStatus }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="getPriceList.pageNum"
        :limit.sync="getPriceList.pageSize"
        @pagination="handleGetPurchaseList"
      />
    </div>
  </div>
</template>

<script>
import { queryTPurPatList,
  purpatUnAudit, reviewPurPat,
  purpatAuSubmit, purpatSubmit,
  undisablePurpat, disablePurpat,
  deletePurpat
} from '@/api/modifyPriceManagement/modifyPriceList'
import search from '@/components/Search/index'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import { toMxAmina, Disable } from '@/components/ToMxamineState'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'ModifyPriceList',
  components: {
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      isTableVis: false,
      ftype: 5,
      total: 0, // 总条目
      getPriceList: {
        pageNum: 1, // 当前页
        pageSize: 200 // 每页显示多少条数据
      },
      tableHeader: [
        { label: this.$t('market.billNo'), type: 'tag', prop: 'fbillNo', width: '110' },
        { label: this.$t('market.patName'), prop: 'patName' },
        { label: this.$t('market.suppliername'), prop: 'fsupplier', align: 'left' },
        { label: this.$t('market.paReason'), prop: 'fdataValue', width: '80' },
        { label: this.$t('market.documentstatus'), prop: 'fdocumentStatus', type: 'state', width: '80' },
        { label: this.$t('market.fmaterial'), prop: 'fnumber', type: 'order', width: '110' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('shop.name1'), prop: 'fname' },
        { label: this.$t('market.specify'), prop: 'fspecificaTion' },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.currency2'), prop: 'fcurrency', width: '80' },
        { label: this.$t('market.afterPrice'), prop: 'fafterprice', align: 'right', width: '80' },
        { label: this.$t('market.afterTaxPrice'), prop: 'faftertaxprice', align: 'right', width: '120' },
        { label: this.$t('market.afterRate'), prop: 'faftertaxrate', align: 'right', width: '80' },
        { label: this.$t('market.effectiveDate'), prop: 'feffectiveDate', width: '100' },
        { label: this.$t('market.forbidStatu'), prop: 'fforbidStatus', type: 'states', width: '100' }
      ],
      // 表格数据
      tableData: [],
      tableHeader1: [
        { label: this.$t('market.fdate'), prop: 'fdate' },
        { label: this.$t('market.note'), prop: 'fnote' },
        { label: this.$t('market.descrip'), prop: 'fdescripTion' },
        { label: this.$t('market.priceList'), prop: 'fpriceList' },
        { label: this.$t('market.topPrice'), prop: 'fupprice' },
        { label: this.$t('market.downPrice'), prop: 'fdownprice' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' },
        { label: this.$t('market.modifier'), prop: 'fmodifier' },
        { label: this.$t('market.modifyDate'), prop: 'fmodifyDate' },
        { label: this.$t('market.approver'), prop: 'fapprover' },
        { label: this.$t('market.approveDate'), prop: 'fapproveDate' },
        { label: this.$t('market.forbider'), prop: 'fforbider' },
        { label: this.$t('market.forbidDate'), prop: 'fforbidDate' },
        { label: this.$t('market.expiryDate'), prop: 'fexpiryDate' }
      ],
      tableHeight: '75vh'
    }
  },
  created() {
    this.handleGetPurchaseList()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '74vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryTPurPatList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array.map(item => {
                return (toMxAmina(item), Disable(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '调价列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取列表数据
    async handleGetPurchaseList(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const { data: RES } = await queryTPurPatList({
        ...this.getPriceList,
        search: this.searCollData.concat(this.details)
      })
      const { array, total } = RES
      this.tableData = array.map(item => {
        return (toMxAmina(item), Disable(item))
      })
      this.total = total
      this.loading = false
    },
    // 新增调价
    addPurchase() {
      this.$router.push({ path: '/createModifyPrice' })
    },
    // 搜索
    handleQueryUntreated(ev) {
      this.getPriceList.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetPurchaseList(ev.detail)
      } else {
        this.handleGetPurchaseList()
      }
    },
    // 成品 原材料
    handleQueryBomList1(ev) {
      this.searCollData = []
      this.pageNum = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetPurchaseList()
    },
    // 详情调价
    detailPurchase(ev) {
      const fid = ev.fid
      this.$router.push({ path: `/editModifyPrice/${fid}` })
    },
    selectionDatas(val) {
      this.val = val
      this.selectFbi = []
      val.map(res => {
        res.fbillNo && this.selectFbi.push(res.fbillNo)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.selectFbi }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetPurchaseList()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewPurPat : purpatUnAudit
      this.toExamineFun(api)
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? purpatSubmit : purpatAuSubmit
      this.toExamineFun(api)
    },
    // 禁用
    using(ev) {
      const api = ev === 1 ? disablePurpat : undisablePurpat
      this.toExamineFun(api)
    },
    // 删除
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deletePurpat
        this.toExamineFun(api)
      })
    },
    // 套打
    queryReportForm1(ev) {
      const fbi = this.selectFbi.join(',')
      if (ev === 1) {
        this.$router.push({ path: `/modifyPriceTao1/${fbi}` })
      } else {
        this.$router.push({ path: `/modifyPriceLian1/${fbi}` })
      }
    },
    // 复制
    copyOrder() {
      this.$router.push({ path: '/createModifyPrice',
        query: {
          fid: this.val[0].fid
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
